﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using UnityEngine;


//继承
public class DefineTest:IXmlSerializable
{
    public int num;
    public string name;

    //无太多意义，返回null即可
    public XmlSchema GetSchema()
    {
        return null;
    }


    //自定义读的规则
    public void ReadXml(XmlReader reader)
    {
        //读属性，可以直接通过索引的方式进行，因为存储的是string，所以要转类型
        //num = int.Parse(reader["num"]);
        //name = reader["name"];

        //读节点
        //这个会特殊点，因为默认是从根节点开始
        //而每次reader.Read() 都只会一块<>一块<>读取

        //方式一
        //reader.Read();  //执行后，指针在<num>
        //reader.Read();  //执行后，指针在num的数值部分
        //num = int.Parse(reader.Value);
        //reader.Read();  //执行后，指针在</num>
        //reader.Read();  
        //reader.Read();
        //name = reader.Value;

        //方式二
        //while (reader.Read())
        //{
        //    //说明此时指针指向的是数值前面的<>
        //    if (reader.NodeType == XmlNodeType.Element)
        //    {
        //        if (reader.Name == "num")
        //        {
        //            reader.Read();  
        //            num = int.Parse(reader.Value);
        //        }
        //        else if (reader.Name == "name")
        //        {
        //            reader.Read();
        //            name = reader.Value;
        //        }
        //    }
        //}

        //读取包裹节点
        //先跳过头部
        reader.Read();
        XmlSerializer xs = new XmlSerializer(typeof(int));
        reader.ReadStartElement("num");
        num = (int)xs.Deserialize(reader);
        reader.ReadEndElement();

        xs = new XmlSerializer(typeof(string));
        reader.ReadStartElement("name");
        name = xs.Deserialize(reader).ToString();
        reader.ReadEndElement();
    }

    //自定义写的规则
    public void WriteXml(XmlWriter writer)
    {
        //写属性
        //writer.WriteAttributeString("num", num.ToString());
        //writer.WriteAttributeString("name", name);


        //写节点
        //writer.WriteElementString("num", num.ToString());
        //writer.WriteElementString("name", name);

        //写包裹节点
        //利用serializer对普通类型进行序列化
        XmlSerializer xs = new XmlSerializer(typeof(int));
        writer.WriteStartElement("num");
        xs.Serialize(writer, num);
        writer.WriteEndElement();

        xs = new XmlSerializer(typeof(string));
        writer.WriteStartElement("name");
        xs.Serialize(writer, name);
        writer.WriteEndElement();

    }
}



public class DefineSerialable : MonoBehaviour
{
    private void Start()
    {
        string path = Application.persistentDataPath + "/DefineTest.xml";
        print(Application.persistentDataPath);

        DefineTest dt = new DefineTest();
        dt.name = "Ame";

        //写
        using (StreamWriter sw = new StreamWriter(path))
        { 
            XmlSerializer xs = new XmlSerializer(typeof(DefineTest));
            xs.Serialize(sw, dt);
        }

        //读
        DefineTest dt2 = new DefineTest();
        if (File.Exists(path))
        {
            using (StreamReader sr = new StreamReader(path))
            {
                XmlSerializer xs = new XmlSerializer(typeof(DefineTest));
                dt2 = xs.Deserialize(sr) as DefineTest;
            }
        }
    }
}
